package easy.id_501_600;

// 5.22写
public class Lee541 {
    public static void main(String[] args) {
        String s="abcdefg";
        System.out.println(reverseStr(s, 2));
    }
    static public String reverseStr(String s, int k) {
        int index=0;
        StringBuffer sb=new StringBuffer(s);
        while(index<=s.length()-1 && index+k-1<=s.length()-1){
            swapByIndex(sb,index,index+k-1);
            index+=2*k;
        }
        if(index<=s.length()-1){
            swapByIndex(sb,index,s.length()-1);
        }
        return sb.toString();
    }

    static public void swapByIndex(StringBuffer s, int i, int j) {  //传递一个stringbuffer，反转i到j闭区间内的字符串
        int l=i;
        int r=j;
        while(l<=r){
            char c=s.charAt(l);
            s.setCharAt(l,s.charAt(r));
            s.setCharAt(r,c);
            l++;
            r--;
        }
    }


}
